<template>
	<view class="myWork">
		<view class="" style="position: fixed;top: 0;left: 0;width: 750rpx;z-index: 99999;" v-if="ceShow">
			<u-notice-bar mode="horizontal" :list="noticeList"></u-notice-bar>
		</view>
		<u-subsection :list="tabList" :current="0" @change="tabChange"></u-subsection>
		<view class="myWorkList" v-if="activeIndex !=3">
			<view class="myWorkItem" v-for="(item,index) in list" :key="index">
				<view class="myWorkItem_left">
					{{item.content}}
				</view>
				<view class="myWorkItem_right">
					<span>{{timestamp(item.createtime)}}</span>
					<u-button type="error" size="mini" @click="goDel(item)">删除</u-button>
				</view>
			</view>
		</view>
		<view class="myWorkList" v-else>
			<view class="myWorkItem" v-for="(item,index) in list" :key="index" @click="goWorkDetail(item)">
				<view class="myWorkItem_left">
					{{item.title}}
				</view>
				<view class="myWorkItem_right">
					{{timestamp(item.createtime)}}
				</view>
			</view>
		</view>
		<view class="" style="padding-top: 40rpx;">
			<u-loadmore :status="status" />
		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex';
	import {
		getMyWork,
		getMyWorkDel
	} from '@/api/index.js'
	export default {
		data() {
			return {
				status: 'loadmore',
				page: 1,
				limit: 10,
				activeIndex: 0,
				list: [],
				tabList: [{
						name: '相亲活动',
						value: 'activity'
					},
					{
						name: '好男好女',
						value: 'kuaibao'
					},
					{
						name: '袜叔推荐',
						value: 'washu'
					},
					{
						name: '拍客',
						value: 'paike'
					},
					{
						name: '拍客回复',
						value: 'paike_reply'
					},
				],
				ceShow: false,
				noticeList: []
			}
		},
		watch: {
			socketMessage(newVal, oldVal) {
				if (newVal) {
					this.ceShow = true
					this.noticeList = [newVal.slice(0, -1)]
					setTimeout(() => {
						this.ceShow = false
					}, 10000)
				}
			}
		},
		computed: {
			...mapGetters(['getSocketMessage']),
			socketMessage() {
				return this.getSocketMessage;
			}
		},
		methods: {
			goDel(item) {
				let that = this
				uni.showModal({
					title: '提示',
					content: '确认删除该发布吗',
					success: function(res) {
						if (res.confirm) {
							let data = {
								type: that.tabList[that.activeIndex].value,
								id: item.id
							}
							getMyWorkDel(data).then(res => {
								uni.showToast({
									icon: "none",
									title: res.msg
								})
								that.page = 1
								that.list = []
								that.getList()
							})
						} else if (res.cancel) {

						}
					}
				})
			},
			goWorkDetail(item) {
				// console.log(item);
				uni.navigateTo({
					url: `/pages/views/worksDetail?id=${item.id}`
				})
			},
			timestamp(timestamp) {
				var date = new Date(timestamp * 1000); // 转换为毫秒
				var year = date.getFullYear();
				var month = ('0' + (date.getMonth() + 1)).slice(-2);
				var day = ('0' + date.getDate()).slice(-2);
				var hours = ('0' + date.getHours()).slice(-2);
				var minutes = ('0' + date.getMinutes()).slice(-2);
				var seconds = ('0' + date.getSeconds()).slice(-2);

				return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
			},
			async getList() {
				this.status = 'loading'
				let data = {
					type: this.tabList[this.activeIndex].value,
					page: this.page,
					limit: this.limit
				}
				const res = await getMyWork(data)
				// console.log(res);
				this.list = [...this.list, ...res.data.data]
				if (res.data.data.length < this.limit) {
					this.status = 'nomore'
				} else {
					this.status = 'loadmore'
				}
			},
			tabChange(e) {
				this.status = 'loadmore'
				this.activeIndex = e
				this.page = 1
				this.list = []
				this.getList()
			}
		},
		onLoad() {
			this.ws.pageFun(this.pageDataLoad, this);
			this.getList()
		},
		onReachBottom() {
			if (this.status == 'nomore') return
			this.page++
			this.getList()
		}
	}
</script>

<style scoped lang="scss">
	.myWork {
		min-height: 100vh;
		background-color: #fff;
		padding: 30rpx;

		.myWorkList {
			.myWorkItem {
				padding: 40rpx 0;
				border-bottom: 1rpx solid #e2e2e2;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.myWorkItem_left {
					max-width: 350rpx;
					text-overflow: ellipsis;
					overflow: hidden;
					white-space: nowrap;
					font-size: 26rpx;
				}

				.myWorkItem_right {
					font-size: 22rpx;
					color: #999;
					display: flex;
					align-items: center;

					span {
						margin-right: 10rpx;
					}
				}
			}
		}
	}
</style>